package com.app.twth.ui.lpnphoto.submit;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseCameraPhotoActivity;
import com.app.twth.bean.FilePathListDTO;
import com.app.twth.bean.PhotoBean;
import com.app.twth.databinding.ActivityLpnphotoSubmitBinding;
import com.app.twth.ui.checkin.PhotoAdapter;
import com.app.twth.ui.lpnphoto.lpn.PhotoInfo;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class LPNPhotoSubmitActivity extends BaseCameraPhotoActivity<ActivityLpnphotoSubmitBinding, LPNPhotoSubmitViewModel> {
    private String TAG = getClass().getSimpleName();

    private int position = 0;
    private PhotoInfo tallyQr = new PhotoInfo();
    private List<PhotoBean> photo1Paths = new ArrayList<>();
    private PhotoAdapter photo1Adapter;

    @Override
    protected String setTitle() {
        return "LPNPhoto(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setResetGone();
        setNext(getString(R.string.submit));

        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        tallyQr = (PhotoInfo) bundle.getSerializable("PhotoInfo");
        binding.tvContainerNo.setText(tallyQr.getContainerNo());
        binding.tvIblpn.setText(tallyQr.getIbLpn());

        photo1Paths.add(new PhotoBean());
        photo1Adapter = new PhotoAdapter(this, photo1Paths, 0, position -> {
            this.position = position;
            requestCamera("photo1");
        }, str -> {
        });
        GridLayoutManager photo1Manager = new GridLayoutManager(this, 3);
        binding.rvPhoto1.setLayoutManager(photo1Manager);
        binding.rvPhoto1.setAdapter(photo1Adapter);


        List<FilePathListDTO> filePathList = tallyQr.getFilePathList();
        for (FilePathListDTO filePathListDTO : filePathList) {
            List<String> pathList = filePathListDTO.getPathList();
            if (pathList != null && pathList.size() > 0) {
                for (String str : pathList) {
                    setPath(str);
                }
            }
        }
    }

    @Override
    protected void initListener() {
        viewModel.fileUploadMutableLiveData.observe(this, data -> {
            Log.e(TAG, "type():" + data.getType() + ",position:" + data.getPosition());
            if (photo1Paths.size() > data.getPosition()) {
                PhotoBean photoBean = photo1Paths.get(data.getPosition());
                photoBean.setUrlImg(data.getPath());
            }

            if (data.isSubmit()) {
                submitData();
            }
        });
        viewModel.lpnPhotoSubmitMutableLiveData.observe(this, data -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                submitData();
                break;
        }
    }

    @Override
    protected void getImgPath(String path) {
        setPath(path);
    }

    private void setPath(String pathImage) {
        Log.e(TAG, "setPath,pathImage:" + pathImage);
        if (!pathImage.contains("http")) {//选择的图片
            String fileName = "lpnphoto";
            viewModel.fileUpload(tallyQr.getContainerNo(), fileName, pathImage, position, false, false);
        }
        PhotoBean imgUrl = new PhotoBean();
        imgUrl.setPathImg(pathImage);
        imgUrl.setUrlImg(pathImage);
        photo1Paths.add(position, imgUrl);

        if (!Utils.isEmpty(photo1Paths.get(photo1Paths.size() - 1).getPathImg())) {
            photo1Paths.add(new PhotoBean());
        }

        if (photo1Adapter != null) {
            photo1Adapter.setData(photo1Paths);
        }
    }

    private void submitData() {
        if (System.currentTimeMillis() - timeMillis < 2000) {
            return;
        }
        timeMillis = System.currentTimeMillis();

        List<String> lpnPathList = new ArrayList<>();
        if (photo1Paths.size() > 0) {
            for (int i = 0; i < photo1Paths.size(); i++) {
                PhotoBean photoBean = photo1Paths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(tallyQr.getContainerNo(), "photo1", s, position, true, true);
                        return;
                    }
                    lpnPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(tallyQr.getContainerNo(), "photo1", photoBean.getPathImg(), position, true, true);
                    return;
                }
            }
        }
        if (lpnPathList.size() == 0) {
            ToastUtil.showToast("Please add lpn photos");
            return;
        }

        PhotoInfo containerInfo = new PhotoInfo();
        containerInfo.setOrderId(tallyQr.getOrderId());
        containerInfo.setIbLpn(tallyQr.getIbLpn());
        List<FilePathListDTO> filePathList = new ArrayList<>();

        FilePathListDTO lpnFilePathListDTO = new FilePathListDTO();
        lpnFilePathListDTO.setName("lpnPhoto");
        lpnFilePathListDTO.setPathList(lpnPathList);
        filePathList.add(lpnFilePathListDTO);
        containerInfo.setFilePathList(filePathList);

        viewModel.lpnPhotoSubmit(containerInfo);
    }
}
